from tkinter import ttk, font, messagebox
import tkinter as tk

_ret = None

def win_main(callBacks: list):
    win = tk.Tk()
    win.title('bdTalk')
    win.geometry('650x600')
    win.resizable(tk.FALSE, tk.TRUE)
    def close():
        callBacks[1]()
        win.destroy()
    win.protocol('WM_DELETE_WINDOW', close)
    f = font.Font(root=win, name='TkTextFont', exists=True)
    f['size'] = 11
    enteryframe = ttk.Frame(win, padding=(5, 5, 5, 5))
    enteryframe.pack(fill=tk.BOTH, side=tk.BOTTOM)
    textframe = ttk.Frame(win, padding=(5, 5, 0, 0))
    # noinspection PyTypeChecker
    # mainframe.grid(column=0, row=0, sticky=(tk.N, tk.E, tk.W, tk.S))
    textframe.pack(fill=tk.BOTH)
    tex0 = tk.Text(textframe, width=69, height=50, font=f)
    # noinspection PyTypeChecker
    tex0.pack(fill=tk.BOTH, side=tk.LEFT)
    sba0 = tk.Scrollbar(textframe)
    sba0.pack(side=tk.RIGHT, fill=tk.Y)
    sba0.config(command=tex0.yview)
    tex0.configure(yscrollcommand=sba0.set)
    tex0.tag_config('tag0', foreground='blue')
    tex0.tag_config('tag1', foreground='black')
    tex0.tag_config('tag2', foreground='green')
    tex0.tag_config('tag3', foreground='red')
    tex0.config(state=tk.DISABLED)

    var0 = tk.Variable()
    etr0 = ttk.Entry(enteryframe, width=60, textvariable=var0)
    etr0.pack(side=tk.LEFT, fill=tk.BOTH)

    def btn0Callback(e=None):
        get = var0.get()
        if 0 < len(get) < 50:
            callBacks[0](get)
            tex0.yview_moveto(1.0)
        var0.set('')

    def recvCallback(msg: dict, nameSelf):
        add = msg['name'] + ': ' + msg['content'] + '\n'
        mov0 = tex0.yview()[1]
        tex0.config(state=tk.NORMAL)
        tagname = 'tag1'
        if msg['name'] == nameSelf:
            tagname = 'tag0'
        elif msg.get('todo', '') == 'welcome':
            tagname = 'tag2'
        elif msg.get('todo', '') == 'bye':
            tagname = 'tag3'
        tex0.insert(tk.END, add, tagname)
        tex0.config(state=tk.DISABLED)
        if mov0 == 1.0:
            tex0.yview_moveto(1.0)

    def setActiveCount(msg: dict):
        win.title('bdTalk-在线 {} 人'.format(msg.get('content', 1)))

    callBacks.append(recvCallback)
    callBacks.append(setActiveCount)

    btn0 = ttk.Button(enteryframe, text='SEND', command=btn0Callback)
    btn0.pack(side=tk.RIGHT, fill=tk.BOTH)
    etr0.bind('<Key-Return>', btn0Callback)
    win.mainloop()

def win_init():
    global _ret
    win = tk.Tk()
    win.title('bdTalk-连接服务器')
    win.geometry('380x92')
    win.resizable(tk.FALSE, tk.FALSE)
    def showmessage():
        global _ret
        _ret = (False,)
        win.destroy()
    win.protocol('WM_DELETE_WINDOW', showmessage)
    mainframe = ttk.Frame(win)
    mainframe.pack(fill=tk.BOTH, pady=5, padx=5)
    fra0 = ttk.Frame(mainframe)
    fra0.pack(fill=tk.X, side=tk.TOP)
    lab0 = ttk.Label(fra0, text='服务器地址')
    lab0.pack(side=tk.LEFT)
    var0 = tk.Variable(win)
    var0.set('www.majjcom.site')
    etr0 = ttk.Entry(fra0, textvariable=var0, width=30)
    etr0.pack(side=tk.RIGHT, fill=tk.X)
    fra1 = ttk.Frame(mainframe)
    fra1.pack(fill=tk.X, side=tk.TOP)
    lab1 = ttk.Label(fra1, text='填写昵称')
    lab1.pack(side=tk.LEFT)
    var1 = tk.Variable(win)
    etr1 = ttk.Entry(fra1, textvariable=var1, width=30)
    etr1.pack(side=tk.RIGHT, fill=tk.X)
    etr0.bind('<Key-Return>', lambda e: etr1.focus_set())
    fra2 = ttk.Frame(mainframe)
    fra2.pack(side=tk.BOTTOM, fill=tk.X)

    def btn0Callback(e=None):
        get0 = str(var0.get())
        get1 = str(var1.get())
        if len(get0) == 0 or len(get1) == 0:
            messagebox.showwarning(title='警告', message='请正确填写内容...')
            return
        if len(get1) > 8:
            messagebox.showwarning(title='警告', message='昵称过长...\n请填写小于8个字的昵称...')
            return
        if get1 == 'System':
            messagebox.showwarning(title='警告', message='禁止填写此名称...')
            return
        global _ret
        _ret = (True, get0, get1)
        win.destroy()

    btn0 = ttk.Button(fra2, text='连接', command=btn0Callback)
    btn0.pack(side=tk.RIGHT)
    etr1.bind('<Key-Return>', btn0Callback)

    def btn1Callback():
        global _ret
        _ret = (False,)
        win.destroy()

    btn1 = ttk.Button(fra2, text='退出', command=btn1Callback)
    btn1.pack(side=tk.LEFT)
    win.mainloop()
    return _ret


def win_err(info: str):
    messagebox.showerror(title='错误', message=info)
